package xin.common.io;

import lombok.AllArgsConstructor;

import java.io.IOException;
import java.io.OutputStream;

/**
 * <pre>
 * 输出限速流
 * </pre>
 *
 * @author lixin_ma@outlook.com
 * @version 1.0
 * @since 2019/4/26
 */
@AllArgsConstructor
public class LimitOutputStream extends OutputStream{
    private OutputStream os;
    private BandwidthLimiter bandwidthLimiter;

    public void write(int b) throws IOException {
        if (bandwidthLimiter != null)
            bandwidthLimiter.limitNextBytes();
        this.os.write(b);
    }

    public void write(byte[] b, int off, int len) throws IOException {
        if (bandwidthLimiter != null)
            bandwidthLimiter.limitNextBytes(len);
        this.os.write(b, off, len);
    }

}
